// pages/my-wallet/my-wallet.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    showDialog:false,

    page: 1,
    limit: 10,
    beanList: [],
    totalPages: 1,
    dealType: 0
  },
  //获取数据
  getData() {
    let that = this;
    util.request(api.Wallet).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          bean: res.data,
          totBalance: res.data.totBalance.toFixed(2),
          canUseBalance: res.data.canUseBalance.toFixed(2),
          profits: res.data.profits.toFixed(2),
          canWithdrawAmt: res.data.canWithdrawAmt.toFixed(2),
          freezeProfits: res.data.freezeProfits.toFixed(2),
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //账户余额说明
  onHint(e){
    this.setData({
      showDialog:true
    })
  },
  onCloseDialog(){
    this.setData({
      showDialog:false
    })
  },
  //余额变更记录
  getBalanceData(dealType) {
    let that = this;
    util.request(api.BalanceLog, {
      dealType: dealType,
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          beanList: that.data.beanList.concat(res.data.list),
          totalPages: res.data.pages
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },

  //tab监听
  onChange(e) {
    var name = e.detail.name;
    this.setData({
      dealType: name,
      page: 1,
      beanList: [],
      totalPages: 1
    })
    this.getBalanceData(name)
  },
  //跳转到收益明细
  onDetails(e) {
    wx.navigateTo({
      url: '/pages/earn-details/earn-details',
    })
  },
  //跳转充值界面
  onRecharge(e) {
    wx.navigateTo({
      url: '/pages/recharge-center/recharge-center',
    })
  },
  //跳转提现界面
  onDeposit(e) {
    console.log("金额--", this.data.bean.canWithdrawAmt)
    util.toClass('/pages/deposit/deposit', {
      money: this.data.bean.canWithdrawAmt
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
    this.getBalanceData(this.data.dealType)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getData(this.data.dealType)
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})